﻿package cn.flashk.zipPack
{
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;

    public class FileTxtZiperDecoder extends EventDispatcher
    {
        private var uldr:URLLoader;
        private var req:URLRequest;
        private var _data:String;
        private var byteArray:ByteArray;
        public static const DECODE_COMPLETE:String = "decodeComplete";

        public function FileTxtZiperDecoder()
        {
            return;
        }// end function

        public function get data() : String
        {
            return _data;
        }// end function

        public function loadAndDecodeFile(url:String) : void
        {
            uldr = new URLLoader();
            uldr.addEventListener(Event.COMPLETE, decodeFile);
            uldr.dataFormat = URLLoaderDataFormat.BINARY;
            req = new URLRequest(url);
            uldr.load(req);
            return;
        }// end function

        private function decodeFile(event:Event) : void
        {
            byteArray = new ByteArray();
            byteArray.writeBytes(uldr.data);
            byteArray.uncompress();
            _data = byteArray.readUTFBytes(byteArray.length);
            uldr = null;
            req = null;
            this.dispatchEvent(new Event(DECODE_COMPLETE));
            return;
        }// end function

    }
}
